package com.wing.cabinet.controller.app;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.wing.cabinet.model.response.EnterpricceIdVO;
import com.wing.cabinet.service.EnterpricceIdService;
import com.wing.common.utils.HttpGCMUtils;
import com.wing.common.utils.HttpHelper;
import com.wing.common.utils.JsonResult;
import com.wing.enterprise.feign.EnterpriseClient;
import com.wing.platform.feign.PlatformClient;
import com.wing.platform.order.model.entity.SysSettingAdvert;
import com.wing.platform.order.model.response.SysSettingForm;
import com.wing.platform.order.model.response.SysSettingVO;
import com.wing.setting.model.response.SystemOfficialInfoVO;
import com.wing.setting.service.SysSettingService;
import com.wing.web.annotation.RequestObjectFieldIgnore;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.ServletRequest;
import java.util.HashMap;
import java.util.Map;

@Slf4j
@Validated
@Api(value = "Admin-刀具柜系统设置管理", tags = "#/cssabinet/sysSetting")
@RestController("appSysSettingController")
@RequestMapping("cabinet/app/sysSetting")
@RequiredArgsConstructor
public class SysSettingController {
    /*@Value("${com.enterprise.apiKey}")
    private String apiKey;*/

    @Value("${com.enterprise.apiPwd}")
    private String apiPwd;


    @Value("${platform.gatewayUrl}")
    private String url;

    @Autowired
    private PlatformClient platformClient;

    private final EnterpricceIdService enterpricceIdService;

    private final SysSettingService sysSettingService;

    private final EnterpriseClient enterpriseClient;

    /**
     * 查询刀具柜系统设置列表
     */
    @ApiOperation(value = "查询所有刀具柜系统设置列表", notes = "搜索")
    @GetMapping("/getSettingList")
    public JsonResult<SysSettingVO> getSettingList(){
        return JsonResult.success(platformClient.getSettingList());
    }

    /**
     * 根据id关联查询
     */
    @GetMapping("/getSettingById")
    public JsonResult<SysSettingAdvert> getSettingById(@RequestParam("id")Long id){
        //加密操作
        String encrypt = HttpGCMUtils.Encrypt("{}", apiPwd);
        EnterpricceIdVO enterpricceIdVO = enterpricceIdService.getEnterpricceIdVO();
        SysSettingAdvert settingById = platformClient.getSettingById(Long.parseLong(enterpricceIdVO.getEnterpriseId()), encrypt,id);
        if (settingById!=null){
            return JsonResult.success(settingById);
        }else {
            return JsonResult.fail("无数据..");
        }
    }

    /**
     * 修改详情
     */
    @PostMapping("/upDate")
    public JsonResult upDate(@RequestBody SysSettingVO sysSettingVO){
       return platformClient.upDate(sysSettingVO);
    }

    @ApiOperation(value = "添加刀具柜系统设置", notes = "添加")
    @RequestObjectFieldIgnore({"id", "createTime", "remark"})
    @PostMapping("/addSetting")
    public JsonResult addSetting(@RequestBody SysSettingForm settingForm) {
       return platformClient.addSetting(settingForm);
    }

    /**
     * 小程序广告列表
     */
    @GetMapping("/getAdvertList")
    public JsonResult<SysSettingVO> getAdvertList(){
        //如果需要传参   将msg参数替换成该接口参数 需要转为json字符串
        String encrypt = HttpGCMUtils.Encrypt("{}", apiPwd);
        EnterpricceIdVO enterpricceIdVO = enterpricceIdService.getEnterpricceIdVO();
        SysSettingVO vo = platformClient.getAdvertList(Long.parseLong(enterpricceIdVO.getEnterpriseId()), encrypt);
        if (vo!=null){
              return JsonResult.success(vo);
        }else {
              return JsonResult.fail("无数据...");
        }
    }

    /**
     * 根据企业Id查询 广告  柜子面板启动画面
     * @param enterpriseId 企业Id
     * @return
     */
    @GetMapping("/getStartSetting")
    public JsonResult getStartSetting(ServletRequest servletRequest, String channelCode, @RequestParam(value = "enterpriseId",required = false)Long enterpriseId){
        if (url.endsWith("/")){
            url=url.substring(0,url.length());
        }
        EnterpricceIdVO enterpricceIdVO=enterpricceIdService.getEnterpricceIdVO();
        if (enterpricceIdVO!=null){
            channelCode=enterpricceIdVO.getChannelCode();
        }
//        JsonResult result = platformClient.getStartSetting(enterpriseId);
        try {
//            String s;
//            if (enterpriseId != null && enterpriseId>0){
//                 s= HttpHelper.httpGet(url + "/platformCab/cabinet/start/startSetting/getStartSetting?enterpriseId="+ enterpriseId);
//            }else {
//
//                 s = HttpHelper.httpGet(url + "/platformCab/cabinet/start/startSetting/getStartSetting");
//            }

            Integer cabinetType = 0;//0,弹簧柜;1,重力柜;2,云柜
            String s= HttpHelper.httpGet(url + "/supplier/cabinetSp/admin/start/startSetting/getStartSetting?enterpriseCode="+ channelCode+"&cabinetType="+cabinetType);
            JSONObject jsonObject = JSONObject.parseObject(s);
            String d = jsonObject.getString("data");
            JSONObject jsonObject2 = JSONObject.parseObject(d);

            //1.企业端设置广告图:广告替换
            com.wing.setting.model.response.SysSettingVO sysSettingVO = sysSettingService.getVideoByChannelCode(channelCode);
            if (ObjectUtils.isNotEmpty(sysSettingVO) && StringUtils.isNotBlank(sysSettingVO.getAdvertVideo())) {
                //JSONObject jsonObject = JSONObject.parseObject(s);
                //String d = jsonObject.getString("data");
                //JSONObject jsonObject2 = JSONObject.parseObject(d);
                //
                JSONArray jsonArray = jsonObject2.getJSONArray("advert");
                for (int i = 0; i < jsonArray.size(); i++) {
                    ((JSONObject) jsonArray.get(i)).put("advertVideo",sysSettingVO.getAdvertVideo());
                }

                jsonObject2.put("advert",jsonArray);
                //return JsonResult.success(jsonObject2);
            }

            //2.企业端设置有LOG:替换LOG
            SystemOfficialInfoVO systemOfficialInfoVO = sysSettingService.getSystemOfficialInfo();
            if(null != systemOfficialInfoVO && StringUtils.isNotBlank(systemOfficialInfoVO.getBackgroundLogo())){
                jsonObject2.put("logo",systemOfficialInfoVO.getBackgroundLogo());
            }

            return JsonResult.success(jsonObject2);


        }catch (Exception e){
            e.printStackTrace();
            log.error("getStartSetting err");
        }
       return JsonResult.success();
    }


    @GetMapping("/getStartMv")
    public JsonResult getStartMv(){
        Map map=new HashMap();
//
//        map.put("mvUrl","https://product-1312272031.cos.ap-nanjing.myqcloud.com/888888/Video_2023-03-13_165903.wmv");
        map.put("mvUrl","https://product-1312272031.cos.ap-nanjing.myqcloud.com/888888/93d47735b5dbd8f7e31683ea6adfc9b4.mp4");
        return JsonResult.success(map);
    }

}
